{*
  Object Pascal simple DateTime Object 
  @Author  Maciej Zagozda (2007-2008)
  @Licence Microsoft Reciprocal License (Ms-RL)
  @Website http://www.codeplex.com/intelitechdelphirtns/
}

unit DateTimeObject;

interface

uses
  DateRoutines,
  SysUtils;


type

  TDateTimeObject = class
  private
    fDateTime: TDateTime;
  public
    constructor Create(const aDate: TDateTime); overload;
    constructor Create(const aDay, aMonth, aYear: Word); overload;
    constructor Create(const aHour, aMinute, aSecond, aMilisecond: Word); overload;
    constructor Create(const aMonth, aDay, aYear, aHour, aMinute: Word); overload;

    procedure SetDateTime(const aDateTime: TDateTime);
    function GetDateTime: TDateTime;
    function DateAsString: ShortString;
    function TimeAsString: ShortString;
    function DateTimeAsString: ShortString;

    function GetDay: Word; overload;
    function GetMonth: Word; overload;
    function GetYear: Word; overload;

    function GetHour: Word; overload;
    function GetMinute: Word; overload;
    function GetSecond: Word; overload;
    function GetMilisecond: Word; overload;

    function DayString: ShortString; overload;
    function MonthString: ShortString; overload;
    function YearString: ShortString; overload;

    function HourString: ShortString; overload;
    function MinuteString: ShortString; overload;
    function SecondString: ShortString; overload;
    function MilisecondString: ShortString; overload;

    procedure IncDay; 
    procedure IncDays(const aDays: Word); 
    procedure DecDay;
    procedure DecDays(const aDays: Word); 

    procedure IncMonth;
    procedure IncMonths(const aMonths: Word);
    procedure DecMonth;
    procedure DecMonths(const aMonths: Word);

    procedure SubstractDate(const aObject: TDateTimeObject);  
    procedure AddDate(const aObject: TDateTimeObject);  

    function DayOfWeek: Integer; 
  end;

  TDateTimeNow = class
  public
    function  GetDateTime: TDateTime;

    function DateAsString: ShortString;
    function TimeAsString: ShortString;
    function DateTimeAsString: ShortString;

    function GetDay: Word; overload;
    function GetMonth: Word; overload;
    function GetYear: Word; overload;

    function GetHour: Word; overload;
    function GetMinute: Word; overload;
    function GetSecond: Word; overload;
    function GetMilisecond: Word; overload;

    function DayString: ShortString; overload;
    function MonthString: ShortString; overload;
    function YearString: ShortString; overload;

    function HourString: ShortString; overload;
    function MinuteString: ShortString; overload;
    function SecondString: ShortString; overload;
    function MilisecondString: ShortString; overload;

    function IncDay: TDateTimeObject; 
    function IncDays(const aDays: Word): TDateTimeObject;
    function DecDay: TDateTimeObject;
    function DecDays(const aDays: Word): TDateTimeObject;

    function IncMonth: TDateTimeObject;
    function IncMonths(const aMonths: Word): TDateTimeObject;
    function DecMonth: TDateTimeObject;
    function DecMonths(const aMonths: Word): TDateTimeObject;

    function SubstractDate(const aObject: TDateTimeObject): TDateTimeObject;  
    function AddDate(const aObject: TDateTimeObject): TDateTimeObject;  

    function DayOfWeek: Integer;
  end;

var
  theDateTime: TDateTimeNow;
  
implementation

constructor TDateTimeObject.Create(const aDate: TDateTime);
begin
  fDateTime := aDate;
end;

constructor TDateTimeObject.Create(const aDay, aMonth, aYear: Word);
begin
  fDateTime := encodeDate(aYear, aMonth, aDay);
end;

constructor TDateTimeObject.Create(const aHour, aMinute, aSecond, aMilisecond: Word);
begin
  fDateTime := encodeTime(aHour, aMinute, aSecond, aMilisecond);
end;

constructor TDateTimeObject.Create(const aMonth, aDay, aYear, aHour, aMinute: Word);
begin
  fDateTime := EncodeDate(aYear, aMonth, aDay);
  fDateTime := fDateTime + EncodeTime(aHour, aMinute, 0, 0);
end;

procedure TDateTimeObject.SetDateTime(const aDateTime: TDateTime);
begin
  fDateTime := aDateTime;
end;

function TDateTimeObject.GetDateTime: TDateTime;
begin
  Result := fDateTime;
end;

function TDateTimeObject.DateAsString: ShortString;
begin
  Result := dateToStr(fDateTime);
end;

function TDateTimeObject.TimeAsString: ShortString;
begin
  Result := TimeToStr(fDateTime);
end;

function TDateTimeObject.DateTimeAsString: ShortString;
begin
  Result := dateToStr(fDateTime) + ' ' + TimeToStr(fDateTime);
end;

function TDateTimeObject.GetDay: Word;
var
  m, y: Word;
begin
  DecodeDate(fDateTime, y, m, Result);
end;

function TDateTimeObject.GetMonth: Word;
var
  d, y: Word;
begin
  DecodeDate(fDateTime, y, Result, d);
end;

function TDateTimeObject.GetYear: Word;
var
  wrdD, wrdM: Word;
begin
  DecodeDate(fDateTime, Result, wrdM, wrdD);
end;

function TDateTimeObject.GetHour: Word;
var
  wrdMin, wrdS, wrdMS: Word;
begin
  DecodeTime(fDateTime, Result, wrdMin, wrdS, wrdMS);
end;

function TDateTimeObject.GetMinute: Word;
var
  h, s, ms: Word;
begin
  DecodeTime(fDateTime, h, Result, s, ms);
end;

function TDateTimeObject.GetSecond: Word;
var
  h, min, ms: Word;
begin
  DecodeTime(fDateTime, h, min, Result, ms);
end;

function TDateTimeObject.GetMilisecond: Word;
var
  h, min, s: Word;
begin
  DecodeTime(fDateTime, h, min, s, Result);
end;

function TDateTimeObject.DayString: ShortString;
begin
  Result := IntToStr(GetDay);
end;

function TDateTimeObject.MonthString: ShortString;
begin
  Result := IntToStr(GetMonth);
end;

function TDateTimeObject.YearString: ShortString;
begin
  Result := IntToStr(GetYear);
end;

function TDateTimeObject.HourString: ShortString;
begin
  Result := IntToStr(GetHour);
end;

function TDateTimeObject.MinuteString: ShortString;
begin
  Result := IntToStr(GetMinute);
end;

function TDateTimeObject.SecondString: ShortString;
begin
  Result := IntToStr(GetSecond);
end;

function TDateTimeObject.MilisecondString: ShortString;
begin
  Result := IntToStr(GetMilisecond);
end;

procedure TDateTimeObject.IncDay;
begin
  fDateTime := fDateTime + 1;
end;

procedure TDateTimeObject.IncDays(const aDays: Word);
begin
  fDateTime := fDateTime + aDays;
end;

procedure TDateTimeObject.DecDay;
begin
  fDateTime := fDateTime - 1;
end;

procedure TDateTimeObject.DecDays(const aDays: Word);
begin
  fDateTime := fDateTime - aDays;
end;

procedure TDateTimeObject.IncMonth;
begin
  fDateTime := SysUtils.IncMonth(fDateTime, 1);
end;

procedure TDateTimeObject.IncMonths(const aMonths: Word);
begin
  fDateTime := SysUtils.IncMonth(fDateTime, aMonths);
end;

procedure TDateTimeObject.DecMonth;
begin
  fDateTime := SysUtils.IncMonth(fDateTime, -1);
end;

procedure TDateTimeObject.DecMonths(const aMonths: Word);
begin
  fDateTime := SysUtils.IncMonth(fDateTime, - aMonths);
end;

procedure TDateTimeObject.SubstractDate(const aObject: TDateTimeObject);
begin
  fDateTime := fDateTime - aObject.GetDateTime;
end;

procedure TDateTimeObject.AddDate(const aObject: TDateTimeObject);
begin
  fDateTime := fDateTime + aObject.GetDateTime;
end;

function TDateTimeObject.DayOfWeek: Integer;
begin
  Result := SysUtils.dayOfWeek(fDateTime);
end;

function  TDateTimeNow.GetDateTime: TDateTime;
begin
  Result := Now;
end;

function  TDateTimeNow.DateAsString: ShortString;
begin
  Result := DateToStr(now);
end;

function  TDateTimeNow.TimeAsString: ShortString;
begin
  Result := TimeToStr(now);
end;

function  TDateTimeNow.DateTimeAsString: ShortString;
begin
  Result := DateToStr(Now) + ' ' + TimeToStr(Now);
end;

function TDateTimeNow.GetDay: Word;
var
  m, y: Word;
begin
  DecodeDate(Now, y, m, Result);
end;

function TDateTimeNow.GetMonth: Word;
var
  d, y: Word;
begin
  DecodeDate(Now, y, Result, d);
end;

function TDateTimeNow.GetYear: Word;
var
  d, m: Word;
begin
  DecodeDate(Now, Result, m, d);
end;

function TDateTimeNow.GetHour: Word;
var
  min, s, ms: Word;
begin
  DecodeTime(Now, Result, min, s, ms);
end;

function TDateTimeNow.GetMinute: Word;
var
  h, s, ms: Word;
begin
  DecodeTime(Now, h, Result, s, ms);
end;

function TDateTimeNow.GetSecond: Word;
var
  h, min, ms: Word;
begin
  DecodeTime(Now, h, min, Result, ms);
end;

function TDateTimeNow.GetMilisecond: Word;
var
  h, min, s: Word;
begin
  DecodeTime(Now, h, min, s, Result);
end;

function TDateTimeNow.DayString: ShortString;
begin
  Result := IntToStr(GetDay);
end;

function TDateTimeNow.MonthString: ShortString;
begin
  Result := IntToStr(GetMonth);
end;

function TDateTimeNow.YearString: ShortString;
begin
  Result := intToStr(GetYear);
end;

function TDateTimeNow.HourString: ShortString;
begin
  Result := intToStr(GetHour);
end;

function TDateTimeNow.MinuteString: ShortString;
begin
  Result := IntToStr(GetMinute);
end;

function TDateTimeNow.SecondString: ShortString;
begin
  Result := IntToStr(GetSecond);
end;

function TDateTimeNow.MilisecondString: ShortString;
begin
  Result := IntToStr(GetMilisecond);
end;

function TDateTimeNow.IncDay: TDateTimeObject;
begin
  Result := TDateTimeObject.Create(now + 1);
end;

function TDateTimeNow.IncDays(const aDays: Word): TDateTimeObject;
begin
  Result := TDateTimeObject.Create(Now + aDays);
end;

function TDateTimeNow.DecDay: TDateTimeObject;
begin
  Result := TDateTimeObject.Create(Now - 1);
end;

function TDateTimeNow.DecDays(const aDays: Word): TDateTimeObject;
begin
  Result := TDateTimeObject.Create(Now - aDays);
end;

function TDateTimeNow.IncMonth: TDateTimeObject;
begin
  Result := TDateTimeObject.Create(SysUtils.IncMonth(Now, 1));
end;

function TDateTimeNow.IncMonths(const aMonths: Word): TDateTimeObject;
begin
  Result := TDateTimeObject.Create(SysUtils.IncMonth(Now, aMonths));
end;

function TDateTimeNow.DecMonth: TDateTimeObject;
begin
  Result := TDateTimeObject.Create(SysUtils.IncMonth(Now, -1));
end;

function TDateTimeNow.DecMonths(const aMonths: Word): TDateTimeObject;
begin
  Result := TDateTimeObject.Create(SysUtils.IncMonth(now, - aMonths));
end;

function TDateTimeNow.SubstractDate(const aObject: TDateTimeObject): TDateTimeObject;
begin
  Result := TDateTimeObject.Create(Now - aObject.GetDateTime);
end;

function TDateTimeNow.AddDate(const aObject: TDateTimeObject): TDateTimeObject;
begin
  Result := TDateTimeObject.Create(Now + aObject.GetDateTime);
end;

function TDateTimeNow.DayOfWeek: Integer;
begin
  Result := SysUtils.DayOfWeek(Now);
end;

initialization
  theDateTime := TDateTimeNow.Create;

finalization
  theDateTime.Free;

end.
